package by.imotaeva.printing_services.services.impl;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.imotaeva.printing_services.dataaccess.PaperDao;
import by.imotaeva.printing_services.datamodel.Paper;
import by.imotaeva.printing_services.services.PaperService;

@Service
public class PaperServiceImpl implements PaperService{
	private static final Logger LOGGER = LoggerFactory.getLogger(PaperServiceImpl.class);

	@Inject
	private PaperDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of PaperService is created. Class is: {}", getClass().getName());
	}

	@Override
	public Paper get(Long id) {
		Paper entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Paper entity) {
		if (entity.getId() == null) {
			LOGGER.debug("Save new: {}", entity);
			dao.insert(entity);
		} else {
			LOGGER.debug("Update: {}", entity);
			dao.update(entity);
		}   
		
	}

	@Override
	public void delete(Paper paper) {
		LOGGER.debug("Remove: {}", paper);
		dao.delete(paper.getId());
	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all papers");
		dao.deleteAll();
	}

}
